using UnityEngine;
using System.Collections;



public class PlayerMovement : Character {
	
	const float PROX_PERCENT_FOR_VOLUME = .2f;
	
	//Is the animation currently flipped?
	private bool isFlipped;
	//What is the current clip name?
	private string currentClipName;
	
	override protected void Start () {
		base.Start();
	}
	
	//Update called every frame
	override protected void Update()
	{
		//Update movement axes
		xMovement = Input.GetAxis("Horizontal");
		yMovement = Input.GetAxis("Vertical");
		
		base.Update();
		
		//update the volumes based on proximity to the audioSprites
		updateVolumes();
		
		handleAnimation();
	}
	
	void updateVolumes() {
		foreach(MentalObject mO in mentalItems) {
			//Can reinstate this if I want to track the full proximity range for other reasons
			//What percent of the audible proximity range is the player at?
			/*float percentVolumeProx = mO.Prox - (1 - PROX_PERCENT_FOR_VOLUME);
			float percentVol = percentVolumeProx/PROX_PERCENT_FOR_VOLUME;
			mO.setVolume(percentVol);*/
			mO.setVolume(mO.Prox);
		}
		foreach(MentalObject mO in mentalCharacters) {
			mO.setVolume(mO.Prox);
		}
	}
	
	//Update this character's mental items and love level
	override protected void updateMentalItemsAndLove() {
		//items have no bearing on the character's love	
		foreach(MentalObject mO in mentalItems) {
			updateProx(mO);
		}
	}
	
	
	override protected void updateMentalCharacters() {
		//update the character proximities
		foreach (MentalCharacter mO in mentalCharacters) {
			updateProx(mO);
			love = mO.Prox;
		}
	}
	
	//handle the animation display
	void handleAnimation() {
	
	//Update current clip name
		currentClipName = sprite.CurrentClip.name;
		
		
		if (yMovement > 0)
		{
			if (currentClipName != "back") 
				sprite.Play("back");
		}
		
		else if (yMovement < 0)
		{
			if (currentClipName != "fwd") 
				sprite.Play("fwd");
		}
		
		else if (xMovement < 0)
		{
			if (isFlipped)
			{
				isFlipped = false;
				sprite.FlipX();
			} 
			if (currentClipName != "left") 
				sprite.Play("left");
			
		}
		
		else if (xMovement > 0)
		{
			if (!isFlipped)
			{
				isFlipped = true;
				sprite.FlipX();
			}
			
			if (currentClipName != "left") 
				sprite.Play("left");
			
		}
			
		else if (currentClipName != "idle")
			sprite.Play("idle");
	}
}

